/*
 * @Descripttion:
 * @version:
 * @Author: lfzxs@qq.com
 * @Date: 2023-08-24 09:51:07
 * @LastEditors: lfzxs@qq.com
 * @LastEditTime: 2023-09-14 16:57:12
 */
package services

import (
	"io"

	"gitee.com/g_boot/attachment/utils"
	"go.beyondstorage.io/v5/services"
	"go.beyondstorage.io/v5/types"
)

type BeyondService struct {
	name         string
	ConnStr      string
	store        types.Storager
}

func NewBeyond(name string, connStr string) (*BeyondService,error){
	Store ,err := createStorager(connStr)

	if err != nil {
		return nil, err
	}

	result := &BeyondService{
		name: name,
		ConnStr: connStr,
		store: Store,
	}

	return result,nil
}


func createStorager(connStr string) (store types.Storager,err error){
	return services.NewStoragerFromString(connStr)
}


func (obj *BeyondService)  GetName()string{
	return obj.name
}

func (obj *BeyondService)  SetName(name string){
	obj.name = name
}

func (obj *BeyondService)  FileHash(filedata io.Reader)(hash string,err error){
	hash,err = utils.Md5File(filedata)
	return
}

func (obj *BeyondService)  GetFileKeyFormHash(hash string)(fileKey string,err error){
	return
}

func (obj *BeyondService)  SaveFile(filedata io.Reader, size int64,path string,pairs ...Pair) (fileUrl string,fileKey string,err error){


	wrtlen,err := obj.store.Write(path,filedata,size)

	if(err != nil){
		return "","",err
	}
	if(wrtlen>=0) {return }
	return
}

func (obj *BeyondService) LinkFile(path string,fileKey string)(objectKey string,err error){
	return
}